#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:chengyanan
@file: keyword_case.py
@time: 2019/12/22  10:53 下午
"""
import sys
import os

path = os.path.dirname(os.getcwd())
sys.path.append(path)
from util.excel_util import ExcelUtile
from keyword_selenium.actionMethod import ActionMethod


class KeywordCase:
    def run_main(self):
        self.action_method = ActionMethod()
        handle_excel = ExcelUtile("../config/keyword.xls")
        case_lines = handle_excel.get_lines()
        print(case_lines)
        if case_lines:
            print("走进了")
            for i in range(1, case_lines):
                is_run = handle_excel.get_col_value(i, 3)
                print(is_run)
                if is_run == "yes":
                    method = handle_excel.get_col_value(i, 4)
                    send_value = handle_excel.get_col_value(i, 5)
                    handle_value = handle_excel.get_col_value(i, 6)
                    except_result_method = handle_excel.get_col_value(i, 7)
                    except_rusult = handle_excel.get_col_value(i, 8)
                    # if send_value:
                    self.run_method(method, send_value, handle_value)
                    if except_rusult != "":
                        except_value = self.get_except_result_value(except_rusult)
                        if except_value[0] == "text":
                            result = self.run_method(except_result_method)
                            if except_value[1] in result:
                                handle_excel.write_value(i, "pass")
                            else:
                                handle_excel.write_value(i, "fail")
                        elif except_value[0] == "element":
                            print("except_result_method===", except_result_method)
                            result = self.run_method(except_result_method, except_value[1])
                            print("result---->", result,except_value[2])

                            if result != "":
                                print("走进result")
                                if except_value[2] == result:
                                    print("写入成功")
                                    handle_excel.write_value(i, "pass")
                                else:
                                    print("写入失败")
                                    handle_excel.write_value(i, "fail")

                        else:
                            print("没有esle")
                    else:
                        print("预期结果为空")

    # 获取预期结果值
    def get_except_result_value(self, except_result):
        print("=====", except_result.split("="))
        return except_result.split("=")

    def run_method(self, method, send_value="", hand_value=""):
        method_value = getattr(self.action_method, method)
        print("hand_value------->", hand_value)
        print("method_value======>", method_value)
        if send_value == "" and hand_value != "":
            result = method_value(hand_value)
        elif send_value != "" and hand_value == "":
            result = method_value(send_value)
        elif send_value == "" and hand_value == "":
            result = method_value()
        else:
            result = method_value(send_value, hand_value)
        return result


if __name__ == '__main__':
    keyword = KeywordCase()
    keyword.run_main()
